﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.EstRec;
using System.Data;
using System.Windows;
using System.ComponentModel;
using BAL.EstRec.FDVGrid;
using BAL.EstRec.FDVIndicador;

namespace BAL.EstRec
{
    public class CapturaFdv : INotifyPropertyChanged
    {
        public long unidHqBgtVersion { get; set; }
        public long unidHqBgt { get; set; }

        public string idPos { get; set; }
        public string POS { get; set; }
        public string idRecurso { get; set; }
        public string Recurso { get; set; }
        public int cantidad;
        private bool _isChecked;
        public bool IsModified { get; set; }

        public int Cantidad
        {
            get { return cantidad; }
            set
            {
                if (value != cantidad)
                {
                    this.cantidad = value;
                    this.IsModified = true;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Cantidad"));
                    }

                }
            }
        }

        public static List<CapturaFdv> GetCapturaFDV(long unidHqBgtVersion)
        {
            List<CapturaFdv> captura;
            System.Data.DataTable dt;
            string sqlStmt;
            //Cadena que ejeucta sp
            sqlStmt = "exec SP_ER_V050_GET_FDV "
                + "@unidVersion=@@par1"
                + ", @territorio = '@@territorio'";

            sqlStmt = sqlStmt.Replace("@@par1", unidHqBgtVersion.ToString());

            try
            {

                string territorio = AUTH.Auth.GetTerritorio();
                sqlStmt = sqlStmt.Replace("@@territorio", territorio);

                dt = SrvDB.ExecuteQuery(sqlStmt);

                captura = dt.AsEnumerable().Select(
                            row => new CapturaFdv
                            {
                                unidHqBgt = row.Field<long?>("UNID_FDV_BGT").GetValueOrDefault()
                                ,
                                idPos = String.IsNullOrEmpty(row.Field<string>("POS")) ? "" : row.Field<string>("POS")
                                ,
                                POS = String.IsNullOrEmpty(row.Field<string>("ID_POS")) ? "" : row.Field<string>("ID_POS")
                                ,
                                idRecurso = String.IsNullOrEmpty(row.Field<string>("RECURSO")) ? "" : row.Field<string>("RECURSO")
                                ,
                                Recurso = String.IsNullOrEmpty(row.Field<string>("ID_RECURSO")) ? "" : row.Field<string>("ID_RECURSO")
                                ,
                                cantidad = row.Field<int?>("CANTIDAD").GetValueOrDefault()
                                ,
                                unidHqBgtVersion = row.Field<long?>("UNID_FDV_BGT_VERSION").GetValueOrDefault()
                            }
                        ).ToList();
                return captura;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return null;
        }

        public static CapFdvPivot2Collection GetPivotCapturaFDV(long unidHqBgtVersion, FDVRecursoRule rule, List<Recurso> recursos)
        {
            CapFdvPivot2Collection capFdv = new CapFdvPivot2Collection();
            System.Data.DataTable dt;
            string sqlStmt;
            //Cadena que ejeucta sp
            sqlStmt = "exec SP_GET_PIVOTFDV_BY_UNID "
                + "@unidVersion=@@par1;";

            sqlStmt = sqlStmt.Replace("@@par1", unidHqBgtVersion.ToString());

            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);

                dt.AsEnumerable().ToList().ForEach(
                    row =>
                    {
                        int count = 0;
                        CapFDVIndicador indicador =
                            new CapFDVIndicador(
                                new Pos((row.IsNull("ID_POS")) ? "" : row["ID_POS"].ToString(), (row.IsNull("ID_POS")) ? "" : row["ID_POS"].ToString())
                                , Int32.Parse((row.IsNull("ID_POS")) ? "" : row["TIEMPO_IMPLEMENTACION"].ToString())
                                , Int32.Parse((row.IsNull("ID_POS")) ? "" : row["VISITAS_SUGERIDAS"].ToString())
                                , Int32.Parse((row.IsNull("ID_POS")) ? "" : row["NUM_INICIATIVA"].ToString())
                                , Int32.Parse((row.IsNull("ID_POS")) ? "" : row["NUM_RECURSO"].ToString())
                                , rule
                            );
                        RecursoIndexer capFdvTmp = new RecursoIndexer(new Pos(row[0].ToString(), row[0].ToString()), recursos, rule, indicador);

                        foreach (DataColumn c in dt.Columns)
                        {
                            if (recursos.Exists(r => r.idRecurso == c.ColumnName))
                            {
                                //capFdvTmp.RecursoIndexer[c.ColumnName] = (!row.IsNull(c)) ? Convert.ToInt32(row[c]) : 0;
                                capFdvTmp.noChangeSet(c.ColumnName, (!row.IsNull(c)) ? Convert.ToInt32(row[c]) : 0);
                            }
                            count++;
                        }
                        //capFdvTmp.calculateIndicators();
                        capFdv.Add(capFdvTmp);
                    }
                );
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return capFdv;
        }

        public bool IsChecked
        {
            get { return this._isChecked; }
            set
            {
                if (value != this._isChecked)
                {
                    this._isChecked = value;
                    if (this.PropertyChanged != null)
                        this.PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
                }
            }
        }


        public static void InsertUpdateCapturaFDV(CapturaFdv cap)
        {

            string sqlStmt = "";
            DataTable dt;

            //Cadena que ejeucta sp

            sqlStmt += "EXEC SP_CATALOGOS ";
            sqlStmt += " @IDPOS = '" + cap.POS + "',";
            sqlStmt += " @IDRECURSO = '" + cap.Recurso + "',";
            sqlStmt += " @CANTIDAD = " + cap.Cantidad + ",";
            sqlStmt += " @PUBLISH = 0,";
            sqlStmt += " @VERSION = " + cap.unidHqBgtVersion;

            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);
                if (dt.Rows.Count == 1)
                {
                    //MessageBox.Show(Convert.ToString(dt.Rows[0]["UNID_HQ"]));
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public CapturaFdv()
        {
            this.IsModified = false;
        }
        public CapturaFdv(long unidHqBgtVersion, string idPos, string idRecurso, int Cantidad)
        {
            this.unidHqBgtVersion = unidHqBgtVersion;
            this.idPos = idPos;
            this.idRecurso = idRecurso;
            this.cantidad = Cantidad;
            this.IsModified = false;
        }
        public CapturaFdv(long unidHqBgtVersion, string idPos, string POS, string idRecurso, string Recurso, int Cantidad)
        {
            this.unidHqBgtVersion = unidHqBgtVersion;
            this.idPos = idPos;
            this.POS = POS;
            this.idRecurso = idRecurso;
            this.Recurso = Recurso;
            this.cantidad = Cantidad;
            this._isChecked = false;
            this.IsModified = false;
        }
        public CapturaFdv(long unidHqBgtVersion, long unidHqBgt, string idPos, string idRecurso, int Cantidad)
        {
            this.unidHqBgtVersion = unidHqBgtVersion;
            this.unidHqBgt = unidHqBgt;
            this.idPos = idPos;
            this.idRecurso = idRecurso;
            this.cantidad = Cantidad;
            this.IsModified = false;
        }
        public CapturaFdv(string POS, string Recurso, int Cantidad)
        {
            this.POS = POS;
            this.Recurso = Recurso;
            this.cantidad = Cantidad;
            this.IsModified = false;
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
